package org.skyfree.ghyll.tcard.storage.file;

import java.util.ArrayList;
import java.util.Iterator;

import org.jdom.Element;
import org.skyfree.ghyll.tcard.core.IProcess;
import org.skyfree.ghyll.tcard.core.ITCard;

class XMLUtil 
{
	static Element buildTCard(ITCard card)
	{
		Element rootElement = new Element("TCard");
		
		rootElement.setAttribute("name",card.getItemName());
		if(card.getItemID()!=null)
			rootElement.setAttribute("id",card.getItemID());
		if(card.isCompositeTCard())
		{
			rootElement.setAttribute("type", "composite");
		}
		else rootElement.setAttribute("type", "leaf");
		rootElement.setAttribute("amount", String.valueOf(card.getAmount()));
		rootElement.setAttribute("owner", card.getOwner());
		Element DrawingElement = new Element("Drawing");
		if(card.getBlurprint()!=null)
			DrawingElement.setAttribute("blueprint",card.getBlurprint().getRelativePath());
		if(card.getSketch()!=null)
			DrawingElement.setAttribute("sketch",card.getSketch().getRelativePath());
		rootElement.addContent(DrawingElement);
		Element MaterialElement = new Element("Material");
		if(card.getMaterial()!=null)
		{
			MaterialElement.setAttribute("type", card.getMaterial().getType());
			MaterialElement.setAttribute("weight", String.valueOf(card.getMaterial().getWeight()));
			MaterialElement.setAttribute("spec", card.getMaterial().getSpecification());	
		}
		rootElement.addContent(MaterialElement);
		rootElement.addContent(buildProcess(card.getWorkingProcess()));
		Element RemarklElement = new Element("Remark");
		if(card.getRemark()!=null)RemarklElement.setText(card.getRemark());
		return rootElement;
	}
	
	static Element buildCardTree(ITCard parentCard,boolean expand2Proccess)
	{
		if(parentCard==null)return null;
		else
		{
			Element parent = new Element("TCard");
			parent.setAttribute("name",parentCard.getItemName());
			if(parentCard.getItemID()!=null)
				parent.setAttribute("id",parentCard.getItemID());
			if(parentCard.isCompositeTCard())
			{
				parent.setAttribute("type", "composite");
			}
			else parent.setAttribute("type", "leaf");
			parent.setAttribute("amount", String.valueOf(parentCard.getAmount()));
			parent.setAttribute("owner", parentCard.getOwner());
			if(expand2Proccess)
				parent.setAttribute("finished", String.valueOf(parentCard.isFinished()));
			Iterator<ITCard> sub = parentCard.getSubTCard().iterator();
			while(sub.hasNext())
			{
				ITCard one = sub.next();
				/*if(one.isCompositeTCard())
					parent.addContent(buildCardTree(one,expand2Proccess));
				else parent.addContent(new Element("TCard").setAttribute("name", one.getItemName()));*/
				parent.addContent(buildCardTree(one,expand2Proccess));
			}
			if(expand2Proccess)
			{
				parent.addContent(buildProcess(parentCard.getWorkingProcess()));
			}
			return parent;
		}
			
	}
	
	static Element buildProcess(ArrayList<IProcess> allProcess)
	{
		Element ProcesslElement = new Element("Process");
		if(allProcess!=null)
		{
			Iterator<IProcess> process = allProcess.iterator();
			while(process.hasNext())
			{
				IProcess one = process.next();
				Element element = new Element("Procedure");
				element.setAttribute("operation", one.getOperation());
				element.setAttribute("content", one.getContent());
				element.setAttribute("time", String.valueOf(one.getPlanTime()));
				if(one.isFinished())
				{
					element.setAttribute("finished", String.valueOf(one.isFinished()));
					element.setAttribute("finishTime", one.getFinishDate());
				}
				
				ProcesslElement.addContent(element);
			}
		}
		return ProcesslElement;
	}
}
